<!-- 工位管理 -->
<template>
  <div class="whole">
    <el-container>
      <el-aside width="200px">
        <CommonAside />
      </el-aside>
      <el-container>
        <el-header>
          <ComponentsHeader />
        </el-header>
        <el-main>
          <div class="app-container">
            <!-- 搜索 -->
            <el-form
              :model="queryParams"
              ref="queryForm"
              size="small"
              :inline="true"
              label-width="100px"
            >
              <el-form-item label="工位名称" prop="stationName" class="mar">
                <el-input
                  v-model="queryParams.stationName"
                  placeholder="请输入工位名称"
                  clearable
                />
              </el-form-item>
              <el-form-item label="工位编号" prop="stationNo">
                <el-input
                  v-model="queryParams.stationNo"
                  placeholder="请输入工位编号"
                  clearable
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                  >搜索</el-button
                >
                <el-button
                  icon="el-icon-refresh"
                  size="mini"
                  @click="resetQuery"
                  >重置</el-button
                >
              </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                  >新增</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  >修改</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  >删除</el-button
                >
              </el-col>
            </el-row>
            <!-- 表格内容 -->
            <el-table
              v-loading="loading"
              :data="stationList"
              @selection-change="handleSelectionChange"
              class="msg"
            >
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="id" align="center" prop="id" />
              <el-table-column
                label="工位名称"
                align="center"
                prop="stationName"
              />
              <el-table-column
                label="工位编号"
                align="center"
                prop="stationNo"
              />
              <el-table-column
                label="扫码枪编码-蓝牙编码"
                align="center"
                prop="smqNo"
              />
              <el-table-column
                label="客户端id"
                align="center"
                prop="clientId"
              />
              <el-table-column
                label="扫码枪端口号"
                align="center"
                prop="port"
              />
              <el-table-column
                label="创建时间"
                align="center"
                prop="createTime"
              />
              <el-table-column
                label="最后编辑时间"
                align="center"
                prop="updateTime"
              />
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    >修改</el-button
                  >
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!--            //分页-->
            <div class="block" style="float: right; margin-top: 24px">
              <el-pagination
                v-show="total > 0"
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryParams.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
            <!-- 添加或修改智造双碳-能效预警对话框 -->
            <el-dialog
              :title="title"
              :visible.sync="open1"
              width="550px"
              append-to-body
            >
              <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="150px"
              >
                <el-form-item label="工位名称" prop="stationName">
                  <el-input
                    v-model="form.stationName"
                    placeholder="请输入工位名称"
                  />
                </el-form-item>
                <el-form-item label="工位编号" prop="stationNo">
                  <el-input
                    v-model="form.stationNo"
                    placeholder="请输入工位编号"
                  />
                </el-form-item>
                <el-form-item label="扫码枪编号" prop="smqNo">
                  <el-input
                    v-model="form.smqNo"
                    placeholder="请输入扫码枪编号"
                  />
                </el-form-item>
                <el-form-item label="扫码枪端口号" prop="port">
                  <el-input
                    v-model="form.port"
                    placeholder="请输入扫码枪端口号"
                  />
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="submitForm" type="primary">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </div>
            </el-dialog>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
  
  <script>
import {
  listStation,
  getStation,
  delStation,
  addStation,
  updateStation,
} from "@/api/stationManagement";
import CommonAside from "@/components/CommonAside.vue";
import ComponentsHeader from "@/components/ComponentsHeader.vue";
export default {
  components: {
    CommonAside,
    ComponentsHeader,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 智造双碳-能效预警表格数据
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 5,
        stationName: null,
        stationNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stationName: [
          { required: true, message: "工位名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询智造双碳-能效预警列表 */
    getList() {
      this.loading = true;
      listStation(this.queryParams).then((response) => {
        this.stationList = response.data.data.data;
        this.total = response.data.data.totalRecords;
        this.loading = false;
        console.log(response);
      });
    },
    // 取消按钮
    cancel() {
     this.open=false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stationName: null,
        stationNo: null,
        smqNo: null,
        portNo: null,
        clientId: null,
        createTime: null,
        updateTime: null,
      };
      this.$refs["queryForm"].resetFields();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "添加工位管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open1 = true;
      const id = row.id || this.ids;
      getStation(id).then((response) => {
        this.form = response.data.data;
        
        this.title = "修改工位管理";
      });
    },
    //分页相关方法
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.page = val;
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStation(this.form).then((response) => {
              console.log(response);
              this.$message({
                type: "success",
                message: "修改成功!",
              });
              this.open = false;
              this.getList();
            });
          } else {
            addStation(this.form).then((response) => {
              console.log(response);
              this.$message({
                type: "success",
                message: "修改成功!",
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids.toString();
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delStation(ids).then((res) => {
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            } else {
              this.$message({
                type: "warning",
                message: "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
  
  <style>
.whole {
  background-color: #edf7ff;
}
.el-header {
  padding: 0;
}
.tag-group {
  margin-top: 20px;
  height: 80px;
  background-color: #ffffff;
  border-radius: 2%;
}
.tag-group__title {
  margin-left: 30px;
  margin-right: 70px;
  line-height: 80px;
}
.tag-group_main {
  margin-right: 40px;
  box-shadow: 1px 1px 2px rgba(166, 166, 166, 0.3);
}
.msg {
  margin-top: 15px;
}
.el-pagination {
  float: right;
  margin-top: 10px;
}
.mar {
  margin-left: -30px;
}
</style>
  